package com.snopy.concurrency.atomic;

import lombok.extern.slf4j.Slf4j;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Demonstration
 * @description
 * @email 77912204@qq.com
 * @date 2022/06/28 15:48
 */
@Slf4j
public class Demonstration {
    public static void main(String[] args) throws InterruptedException {
        SimpleNode C = new SimpleNode(3,null);
        SimpleNode B = new SimpleNode(2,C);
        SimpleNode A = new SimpleNode(1,B);

        Thread t1 = new Thread(()->{
            SimpleNode prev = A.next.get();//2
            SimpleNode next = B.next.get();//3

            try {
                Thread.sleep(3000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }

            log.debug("delete 2:{}",A.next.compareAndSet(prev,next));
        });
        t1.start();
        Thread.sleep(2000);
        Thread t2 = new Thread(()->{
            SimpleNode prev = B.next.get();//3
            SimpleNode next = C.next.get();//null
            log.debug("delete 3:{}",B.next.compareAndSet(prev,next));
            //
        });
        t2.start();
        t1.join();
        t2.join();
        SimpleNode root = A;
        while (root!=null){
            log.info("{}",root.value);
            root = root.next.get();
        }
    }
}
